package com.easyaop.api.pointcut;

import java.lang.annotation.Annotation;
import java.util.List;

public class AnnotationPointcut extends Pointcut {
    private final String packageName;

    private final String annotation;

    public AnnotationPointcut(String packageName, String annotation) {
        super(packageName + "@" + annotation);
        this.annotation = annotation;
        this.packageName = packageName;
    }

    public AnnotationPointcut(String packageName, Class<? extends Annotation> annotation) {
        this(packageName, annotation.getName());
    }

    public String getPackageName() {
        return packageName;
    }

    public String getAnnotation() {
        return annotation;
    }

    @Override
    public boolean isClass(ClassObject clz) {
        return packageName == null || clz.getName().startsWith(packageName);
    }

    @Override
    public boolean isMethod(MethodObject method) {
        List<String> annotations = method.getAnnotations();
        return annotations != null && annotations.contains(annotation);
    }

        /*
        Annotation[] annotations = method.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().getName().equals(this.annotation)) {
                return true;
            }
        }
        return false;

         */

}
